#include <iostream>
#include <iomanip>
#include <vector>

#include "Products.h"
#include "Usb.h"
#include "HardDisk.h"
#include "Monitor.h"
#include "SDcard.h"
#include "AppSoftware.h"
#include "PosMenu.h"

using namespace std;


void main()
{

	// create derived-class objects
	// Product - Usb 
	Usb usb("USB07A61", "Toshiba", 56, 59, 4, "BLUE");
	Usb usb2("USB02K9Y", "Kingston", 32, 99, 8, "RED");
	Usb usb3("USB061U8", "Sony", 67, 49, 2, "Silver");

	// Product - Hard Disk
	HardDisk hardDisk("HD00G7R2", "Samsung", 24, 349, 160, 0);
	HardDisk hardDisk2("HD00O184", "Fujitsu", 38, 429, 250, 0);
	HardDisk hardDisk3("HD00W37M", "Hitachi", 47, 479, 250, 1); 
	HardDisk hardDisk4("HD0028HB", "Hitachi", 22, 559, 320, 1);

	// Product - Monitor
	Monitor monitor("MON0I95A", "BenQ", 13, 1299, 17);
	Monitor monitor2("MON0PKL2", "Samsung", 15, 1999, 22);
	Monitor monitor3("MON07X1C", "Sony", 19, 1699, 19);
	Monitor monitor4("MON0151J", "Samsung", 23, 1839, 20);

	// Product - SD Card
	SDcard sdCard("SD0083JK", "Imation", 49, 69, 4, "SD");
	SDcard sdCard2("SD00URT2", "San Disk", 32, 45, 2, "SD");
	SDcard sdCard3("SD004VJ9", "San Disk", 22, 29, 1, "Micro SD");
	SDcard sdCard4("SD00NZ28", "Toshiba", 38, 49, 2, "Mini SD");

	// product - Application Software //
	AppSoftware appSoftware("APP06DK1", "Adobe", 62, 699, "PhotoShop", "English", "CS3");
	AppSoftware appSoftware2("APP0KP9T", "Microsoft", 71, 599, "MS Office", "English", "2007");
	AppSoftware appSoftware3("APP0H283", "Adobe", 44, 899, "Premiere", "English", "CS4");
	AppSoftware appSoftware4("APP099A4", "Norton", 36, 299, "Norton Antivirus", "", "CS3");

	// create vector of base-class pointers
	vector <Products *> products(19);

	products[0] = &usb;
	products[1] = &usb2;
	products[2] = &usb3;
	products[3] = &hardDisk;
	products[4] = &hardDisk2;
	products[5] = &hardDisk3;
	products[6] = &hardDisk4;
	products[7] = &monitor;
	products[8] = &monitor2;
	products[9] = &monitor3;
	products[10] = &monitor4;
	products[11] = &sdCard;
	products[12] = &sdCard2;
	products[13] = &sdCard3;
	products[14] = &sdCard4;
	products[15] = &appSoftware;
	products[16] = &appSoftware2;
	products[17] = &appSoftware3;
	products[18] = &appSoftware4;

	cout  <<  setw(70) << "---POS System---" << endl;
	cout<< endl;
	PosMenu pos;
	pos.setProductsList(products);
	//pos.browseAllItems();
	
	int functionFlag=-1; //  Flag for choosing the function
	while(functionFlag!=12)
	{
		cout<< endl;
		cout << left<<"[0]"<< setw(40)<<"Browse all the items "<<"[1]"<< setw(40)<< "Browse specific type items"<<"[2]" << setw(40)<<"Search an item"<<endl;
		cout << "[3]"<< setw(40)<<"Restock an item "<<"[4]" << setw(40)<<"Sort the items"<<"[5]"<<setw(40)<<"Pick items into shopping cart"<<endl;
		cout << "[6]"<< setw(40)<<"Browse shopping cart's items "<<"[7]"<< setw(40)<< "Clear shopping cart"<<"[8]"<< setw(40)<<"Purchase shopping cart's items"<<endl;
		cout << "[9]"<< setw(40)<<"Invoice checking "<<"[10]"<< setw(39)<<"View sale total"<<"[11]"<< setw(40)<<"Browse items below threshold value"<<endl;
		cout << "[12]"<< setw(40)<<"Exit"<<endl;
		cout << endl;
		cout<< "Please choose a function: ";
		cin >>functionFlag;


		if(functionFlag ==0) // Function 0 - Browse all the items
		{
			cout << "" << endl; 
			pos.browseAllItems();
		}
		else if(functionFlag==1) // Function 1 - Browse specific type items
		{
			string type;
			cout << "Please enter the type"<<endl;
			cin >>type;
			pos.browseSpecificItems(type);
		}
		else if(functionFlag==2) // Function 2 - Search an item
		{
			int searchOption;
			string keyword;
			cout << "Please choose one of the option for searching: "<<endl;
			cout << "[1] Pid [2] Brand [3] Price [4] Stock Quantiy"<<endl;
			cin >> searchOption;


			switch(searchOption)
			{
			case 1:
				cout<< "Please enter the Pid: "<<endl;
				cin >> keyword;
				pos.searchItemByPid(keyword);
				break;
			case 2:
				cout<< "Please enter the Brand: "<<endl;
				cin >> keyword;
				pos.searchItemByBrand(keyword);
				break;
			case 3:
				cout<< "Please enter the price: "<<endl;
				cin >> keyword;
				pos.searchItemByPrice(atoi(keyword.c_str()));
				break;
			case 4:
				cout<< "Please enter the stock quantity: "<<endl;
				cin >> keyword;
				pos.searchItemByStock(atoi(keyword.c_str()));
				break;
			default:
				cout<< "[Warning] Invalid input"<<endl;
			}


		}
		else if(functionFlag==3) // Function 3 - Restock an item
		{
			pos.browseAllItems();
			int itemNo;
			int quantity;
			cout << "Please select product: ";
			cin >> itemNo;
			cout << "Please enter quantity: ";
			cin >> quantity;
			pos.restockItem(pos.getProductsList()[itemNo],quantity);

		}
		else if(functionFlag == 4) // Function 4 - Sort the items
		{
			int sortOption;

			cout << "Please choose one of the option for sorting: "<<endl;
			cout << "[1] Brand [2] Price [3] Stock Quantiy"<<endl;
			cin >> sortOption;

			switch(sortOption)
			{
			case 1:
				pos.sortByBrand();
				break;
			case 2:
				pos.sortByPrice();
				break;
			case 3:
				pos.sortByStock();
				break;

			default:
				cout<< "[Warning] Invalid input"<<endl;
			}
			pos.browseAllItems();
		}
		else if(functionFlag ==5) // Function 5 - Pick items into shopping cart
		{
			pos.browseAllItems();
			pos.selectProductsIntoShoppingCart();
			pos.browseAllItemsInShoppingCart();
		}
		else if(functionFlag ==6) // Function 6 - Browse shopping cart's items
		{
			pos.browseAllItemsInShoppingCart();
		}
		else if(functionFlag == 7) // Function 7 - Clear shopping cart
		{
			pos.clearShoppingCart();
		}
		else if (functionFlag ==8) // Function 8 - Purchase shopping cart's items
		{
			pos.createTransaction();
		}
		else if(functionFlag ==9) // Function 9 - Invoice checking
		{
			pos.viewInvoiceDetail();
		}
		else if(functionFlag ==10) // Function 10 - View sale total
		{
			pos.viewSaleReport();
		}
		else if(functionFlag ==11) // Function 11 - Browse items below threshold value
		{
			pos.browseItemsBelowThresholdValue();
		}



	}

}
